//
//  DetailViewController.m
//  AliBall
//
//  Created by 殷殷明静 on 16/8/15.
//  Copyright © 2016年 殷明静. All rights reserved.
//

#import "DetailViewController.h"
#import "Data_detail.h"
#import "Detail_ali.h"
#import "DetailTableViewCell.h"
#import "SecTableViewCell.h"
#import "ThirdTableViewCell.h"
#import "FourTableViewCell.h"
#import "FifTableViewCell.h"
#import "SixTableViewCell.h"
@interface DetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) Detail_ali *ali;
@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createData];
    [self createView];
    
}
- (void)createView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DetailTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"reuse"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SecTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"reuse1"];
     [self.tableView registerNib:[UINib nibWithNibName:@"ThirdTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"reuse2"];
    [self.tableView registerNib:[UINib nibWithNibName:@"FourTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"reuse3"];
    [self.tableView registerNib:[UINib nibWithNibName:@"FifTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"reuse4"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SixTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"reuse5"];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 128, 0);
    NSLog(@"宽度%f",CGRectGetWidth(self.view.bounds));
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == _ali.data.songList.count + 2 + 1 + 1 ) {
        return 130;
    } else {
        return 40;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _ali.data.songList.count + 2 + 1 + 1 + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        DetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        cell.backgroundColor = [UIColor blackColor];
        return cell;
        
    } else if (indexPath.row == 1) {
        SecTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse1"];
        cell.backgroundColor = [UIColor blackColor];
        return cell;
    } else if (indexPath.row < _ali.data.songList.count + 2) {
        ThirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse2"];
        cell.backgroundColor = [UIColor blackColor];
        cell.label_1.text = [NSString stringWithFormat:@"%ld",indexPath.row - 1];
        cell.label_2.text = _ali.data.songList[indexPath.row - 2].name;
        cell.label_3.text = _ali.data.songList[indexPath.row - 2].alias;
        cell.label_4.text = [NSString stringWithFormat:@"%@ - %@", _ali.data.songList[indexPath.row - 2].singerName, _ali.data.songList[indexPath.row - 2].name];
        return cell;
    } else if (indexPath.row == _ali.data.songList.count + 2){
        FourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse3"];
        cell.label_ymj.text = [NSString stringWithFormat:@"%@人收藏",_ali.data.favoriteCount];
        cell.backgroundColor = [UIColor blackColor];
        return cell;
        
    } else if (indexPath.row == _ali.data.songList.count + 2 + 1){
        FifTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse4"];
        cell.label_ymj.text = [NSString stringWithFormat:@"%@的更多专辑",_ali.data.songList[0].singerName];
        cell.backgroundColor = [UIColor blackColor];
        return cell;
    } else {
        SixTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse5"];
        cell.backgroundColor = [UIColor blackColor];
        return cell;
    }
   
   
}
- (void)createData {
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/album/%@",_value];
    NSLog(@"%@",str);
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       dispatch_async(dispatch_get_main_queue(), ^{
           
           NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:0];
           
           self.ali = [Detail_ali modelWithDic:dic];
           
           
           UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250)];
           imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_ali.data.picUrl]]];
           self.tableView.tableHeaderView = imageView;
           [self.tableView reloadData];
       });
        
    }];
    [task resume];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
